package org.csu.small.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Maps;
import org.apache.commons.lang3.StringUtils;
import org.csu.small.common.Constant;
import org.csu.small.common.UniformResponse;
import org.csu.small.entity.Slide;
import org.csu.small.persistence.SlideMapper;
import org.csu.small.service.ISlideService;
import org.csu.small.utils.QiniuCloudUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author Rambo
 * @date 2020-06-08 9:24
 */
@Service
public class SlideServiceImpl implements ISlideService {

    @Autowired
    private SlideMapper slideMapper;


    @Override
    public UniformResponse<List<Slide>> queryAll(Integer isShow) {
        List<Slide> slideList = slideMapper.selectList(Wrappers.<Slide>query().eq("is_show", isShow).orderByAsc("sort_order"));
        return UniformResponse.createForSuccess(slideList);
    }

    @Override
    public UniformResponse<String> addSlide(Slide slide) {
        int rows = slideMapper.insert(slide);
        if (rows == 0){
            return UniformResponse.createForErrorMessage("添加轮播图失败");
        }
        return UniformResponse.createForSuccessMessage("添加轮播图成功");
    }

    @Override
    public UniformResponse<String> editSlide(Slide slide) {
        slide.setUpdateTime(LocalDateTime.now());
        int rows = slideMapper.updateById(slide);
        if (rows == 0){
            return UniformResponse.createForErrorMessage("修改轮播图信息失败");
        }
        return UniformResponse.createForSuccessMessage("修改轮播图信息成功");
    }

    @Override
    public UniformResponse<Object> getSlideList(int pageNum, int pageSize) {
        Page<Slide> result = new Page<>();
        result.setCurrent(pageNum);
        result.setSize(pageSize);
        QueryWrapper<Slide> query = new QueryWrapper<>();
        result = slideMapper.selectPage(result, query);
        return UniformResponse.createForSuccess(result);
    }

    @Override
    public UniformResponse<String> setSlideStatus(Integer slideId, Integer status) {
        Slide slide = slideMapper.selectById(slideId);
        slide.setIsShow(status);
        int rows = slideMapper.updateById(slide);
        if (rows == 0){
            return UniformResponse.createForErrorMessage("修改状态信息失败");
        }
        return UniformResponse.createForSuccessMessage("修改状态信息成功");
    }

    @Override
    public UniformResponse slide_upload(MultipartFile upload_file, String path) {
        Map<String, String> resultMap = Maps.newHashMap();
        String file_name = upload_file.getOriginalFilename();
        File folder = new File(path);
        if (!folder.exists()) {
            folder.setWritable(true);
            folder.mkdirs();
        }
        String imgPath = path + file_name;
        try {
            upload_file.transferTo(new File(imgPath));
        }catch (Exception e){
            e.printStackTrace();
        }
        if(QiniuCloudUtil.upload(path + file_name, file_name,true)){
            String uri = null;
            try {
                uri = QiniuCloudUtil.fileUrl(file_name);
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            resultMap.put("uri", uri);
            return UniformResponse.createForSuccess(resultMap);
        }
        return UniformResponse.createForSuccessMessage("上传失败");
    }

    @Override
    public UniformResponse<Slide> detail(Integer slideId) {
        Slide slide = slideMapper.selectById(slideId);
        if (slide == null){
            return UniformResponse.createForErrorMessage("该轮播图不存在或删除");
        }
        return UniformResponse.createForSuccess(slide);
    }

    @Override
    public UniformResponse<String> delete(Integer slideId) {
        int rows = slideMapper.deleteById(slideId);
        if (rows == 0){
            return UniformResponse.createForErrorMessage("删除轮播图失败");
        }
        return UniformResponse.createForSuccessMessage("删除轮播图成功");
    }
}
